package com.controller;

import com.domain.*;
import com.service.CourseService;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentService studentService;

    /**
     * 查询已修课程
     * @param request
     * @return
     */
    @RequestMapping("/overCourse")
    public String overCourse(String page, HttpServletRequest request, Model model){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        int stuID = Integer.parseInt(user.getUserName());
        PageBean bean = courseService.stuFindCourse(currentPage,1,stuID);
        model.addAttribute("overCourseList",bean.getList());
        model.addAttribute("pageBean",bean);
        return "student/overCourse";
    }

    /**
     * 模糊查询
     * @return
     */
    @RequestMapping("/searchCourse")
    public String searchCourse(String page,String findByName,HttpServletRequest request,Model model){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        PageBean bean = courseService.findAndSearchCourse(currentPage,findByName,user.getRole(),0);
        model.addAttribute("courses",bean.getList());
        model.addAttribute("pageBean",bean);
        return "student/showCourse";
    }
    /**
     * 显示所有课程
     * @return
     */
    @RequestMapping("/showCourse")
    public String showCourse(String page,Model model){
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        PageBean pageBean = courseService.findAndSearchCourse(currentPage,null,2,0);
        model.addAttribute("courses",pageBean.getList());
        model.addAttribute("pageBean", pageBean);
        return "student/showCourse";
    }

    /**
     * 显示所选课程
     * @param request
     * @return
     */
    @RequestMapping("/selectedCourse")
    public String selectedCourse(String page,HttpServletRequest request,Model model){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int currentPage = 1;
        if (page != null){
            currentPage = Integer.parseInt(page);
        }
        int stuID = Integer.parseInt(user.getUserName());
        PageBean bean = courseService.stuFindCourse(currentPage,0,stuID);
        model.addAttribute("selectCourseList",bean.getList());
        model.addAttribute("pageBean",bean);
        return "student/selectCourse";
    }

    /**
     * 选课
     * @return
     */
    @RequestMapping("/selectCourse")
    public String SelectedCourse(String cid,HttpServletRequest request){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int id = Integer.parseInt(cid);
        SelectCourse course = new SelectCourse();
        course.setCourseID(id);
        course.setStudentID(Integer.parseInt(user.getUserName()));
        course.setMark(0);
        String result = studentService.selectCourse(user.getUserName(),course);
        if (!"success".equals(result)){
            request.setAttribute("message",result);
            return "error";
        }
        int totalAll = studentService.findAllTotal(Integer.parseInt(user.getUserName()));
        //覆盖之前选课总数
        request.getSession().setAttribute("totalAll",totalAll);
        return "redirect:/student/selectedCourse";
    }

    /**
     * 退选
     * @return
     */
    @RequestMapping("/outCourse")
    public String outCourse(String cid,HttpServletRequest request){
        LoginUser user = (LoginUser) request.getSession().getAttribute("user");
        int id = Integer.parseInt(cid);
        SelectCourse course = new SelectCourse();
        course.setCourseID(id);
        course.setStudentID(Integer.parseInt(user.getUserName()));
        studentService.outCourse(course);
        int totalAll = studentService.findAllTotal(Integer.parseInt(user.getUserName()));
        //覆盖之前选课总数
        request.getSession().setAttribute("totalAll",totalAll);
        return "redirect:/student/selectedCourse";
    }

}
